import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import { executeCommandInChildProcess } from './command';

class TreeNode extends vscode.TreeItem {
    constructor(label: string, collapsed: vscode.TreeItemCollapsibleState, contextValue: string, tooltip?: string, iconPath?: string, description?: string) {
        super(label, collapsed);
        this.tooltip = tooltip;
        this.iconPath = iconPath;
        this.contextValue = contextValue;
        this.description = description;
    }
}

export class ToolNode extends TreeNode {
    runCommandString: string;
    constructor(toolName: string, command: string, tooltip?: string, iconPath?: string, description?: string) {
        super(toolName, vscode.TreeItemCollapsibleState.None, 'tool', tooltip, iconPath, description);
        this.runCommandString = command;
    }

    CommandString(): string {
        return this.runCommandString;
    }
}

export class EntendToolsProvider implements vscode.TreeDataProvider<TreeNode> {
    private _onDidChangeTreeData: vscode.EventEmitter<TreeNode | undefined | null> = new vscode.EventEmitter<TreeNode | undefined | null>();
    readonly onDidChangeTreeData: vscode.Event<TreeNode | undefined | null> = this._onDidChangeTreeData.event;
    private rootNodes: TreeNode[];

    refresh() {
        console.log('refresh toolsTree');
        this._onDidChangeTreeData.fire(undefined);
    }

    constructor() {
        this.rootNodes = [
            new ToolNode("rviz2", "rviz2", "run rviz2", path.join(__dirname, "../resource/toolbox.png")),
            new ToolNode("rqt", "rqt", "run rqt", path.join(__dirname, "../resource/toolbox.png")),
        ];
    }

    getTreeItem(element: TreeNode): vscode.TreeItem {
        return element;
    }

    getChildren(element?: TreeNode): Thenable<TreeNode[]> {
        if (!element) {
            return Promise.resolve(this.rootNodes);
        }
        else {
            return Promise.resolve([]);
        }
    }
}